package cn.scxy.bookcity.controller;

import cn.scxy.bookcity.pojo.dto.CommentDTO;
import cn.scxy.bookcity.pojo.dto.UserLoginDTO;
import cn.scxy.bookcity.pojo.vo.CommentListItemVO;
import cn.scxy.bookcity.service.ICommentService;
import cn.scxy.bookcity.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户首页的控制器
 *
 * @author zzr
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论模块")
@Slf4j
public class CommentController {
    @Autowired
    private ICommentService iCommentService;

    @ApiOperation("插入评论")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(CommentDTO  commentDTO) {
        log.debug("接收到的内容为:{}",commentDTO);
        iCommentService.AddNew(commentDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据评论id删除评论")
    @ApiOperationSupport(order = 200)
    @PostMapping("/delete/{id}")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("根据评论id删除评论,参数：{}",id);
        iCommentService.deleteById(id);
        return JsonResult.ok();
    }


    @ApiOperation("根据商品id查询评论数量")
    @ApiOperationSupport(order = 300)
    @GetMapping("/commentCount/{id}")
    public JsonResult<Integer> queryPress1(@PathVariable Long id) {
        System.out.println("商品id---------" + id);
        int count = iCommentService.countComment(id);
        log.debug("查询到:{}条评论", count);
        return JsonResult.ok(count);
    }

    @ApiOperation("查询全部评论")
    @ApiOperationSupport(order = 300)
    @GetMapping("")
    public JsonResult<List<CommentListItemVO>> comments(){
        List<CommentListItemVO> comment = iCommentService.comments();
        return JsonResult.ok(comment);
    }

    @ApiOperation("根据商品id查询评论")
    @ApiOperationSupport(order = 310)
    @GetMapping("/{id}/comments")
    public JsonResult<List<CommentListItemVO>> comment(@PathVariable Long id){
        List<CommentListItemVO> comment = iCommentService.comment(id);
        return JsonResult.ok(comment);
    }

    @ApiOperation("根据用户id查询评论")
    @ApiOperationSupport(order = 320)
    @GetMapping("/commentsByuId")
    public JsonResult<List<CommentListItemVO>> commentsByuId(HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        List<CommentListItemVO> comment = iCommentService.commentByuId(uId);
        return JsonResult.ok(comment);
    }

    @ApiOperation("根据用户昵称查询评论")
    @ApiOperationSupport(order = 330)
    @GetMapping("/commentsByNickName/{nickName}")
    public JsonResult<List<CommentListItemVO>> commentsByNickName(@PathVariable String nickName){
        List<CommentListItemVO> comment = iCommentService.commentByNickName(nickName);
        return JsonResult.ok(comment);
    }

    @ApiOperation("根据书名查询评论")
    @ApiOperationSupport(order = 340)
    @GetMapping("/commentsByName/{name}")
    public JsonResult<List<CommentListItemVO>> commentsByName(@PathVariable String name){
        List<CommentListItemVO> comment = iCommentService.commentByName(name);
        return JsonResult.ok(comment);
    }
}